﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iPal.analysis;
using iPal.lexer;
using iPal.node;
using iPal.parser;


namespace GOLD_TEST
{
    class Program
    {
        static void Main(string[] args)
        {

            Stream st = new FileStream(Directory.GetCurrentDirectory() + "\\testCode.txt", FileMode.Open);
            StreamReader sr = new StreamReader(st);
            Lexer lex = new Lexer(sr);

            Parser parse = new Parser(lex);

            Start ast = parse.Parse();

            ast.Apply(new SemanticAnalyser());
            ast.Apply(new CodeGenerator());

            Console.WriteLine("**** DONE ****");
            Console.ReadLine();

        }
    }
}
#region oldCode
/*
 *          BinaryReader br = new BinaryReader(st);
            gg.LoadTables(br);
            Console.WriteLine(text);
            gg.Open(ref text);
            bool done = false;

            Reduction root = null;


            while (!done)
            {
                #region ParseSwitch
                switch (gg.Parse())
                {
                    case ParseMessage.TokenRead:
                        break;

                    case ParseMessage.Reduction:
                        Reduction cR = (Reduction)gg.CurrentReduction;
                        break;
                    case ParseMessage.Accept:

                        root = (Reduction)gg.CurrentReduction;
                        done = true;
                        break;
                    case ParseMessage.GroupError:
                        done = true;
                        break;
                    case ParseMessage.InternalError:
                        done = true;
                        break;
                    case ParseMessage.LexicalError:
                        done = true;
                        break;
                    case ParseMessage.NotLoadedError:
                        done = true;
                        break;
                    case ParseMessage.SyntaxError:
                        done = true;
                        break;
                    default:
                        done = true;
                        break;
                }
                #endregion
            };

            if (root != null)
            {
                Node rootNode = new Node(root.Parent.Text().Split(':')[0]);
                TraverseParseTree(rootNode, root, 1);
                foreach (Node n in rootNode.Children) Console.WriteLine(n.Data);

            }
            else Console.WriteLine("ERROR");
            //printRoot(rootNode);
            Console.ReadLine();
        }

        static private void TraverseParseTree(Node root, Reduction reduction, int indent)
        {
            //This is a simple recursive procedure that draws an ASCII version of the parse
            //tree

            int n;

            //=== Display the children of the reduction
            for (n = 0; n < reduction.Count(); n++)
            {
                Node child = null;
                switch (reduction[n].Type())
                {
                    case GOLD.SymbolType.Nonterminal:
                        GOLD.Reduction branch = (GOLD.Reduction)reduction[n].Data;
                        string[] dataArray = branch.Parent.Text().Split(':');
                        string data = dataArray[0];
                        child = new Node(data);
                        root.Children.Add(child);
                        child.Parent = root;

                        TraverseParseTree(child, branch, indent + 1);
                        break;

                    default:
                        string leaf = (string)reduction[n].Data;
                        child = new Node(leaf);
                        root.Children.Add(child);
                        child.Parent = root;

                        break;
                }
            }
        }

        static private void printRoot(Node node)
        {
            printNode(node, 0);
        }

        static private void printNode(Node node, int level)
        {
            for (int i = 0; i < level; i++) Console.Write("|-");
            Console.WriteLine(node.Data);
            foreach (Node child in node.Children)
            {
                printNode(child, level + 1); 
            }
        }
    }
}

/*

 * switch (reduction[n].Type())
               {
                   case GOLD.SymbolType.Nonterminal:
                       GOLD.Reduction branch = (GOLD.Reduction)reduction[n].Data;
  
                       tree.AppendLine(indentText + "+-" + branch.Parent.Text(false));
                       DrawReduction(tree, branch, indent + 1);
                       break;

                   default:
                       string leaf = (string)reduction[n].Data;

                       tree.AppendLine(indentText + "+-" + leaf);
                       break;
               }
 * 
*/
#endregion